﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MyCommerce.ManagementBase.Common.Base.Common;
using MyCommerce.Presentation.Stores.Presenters;
using MyCommerce.Presentation.Stores.IViews;
using Innova.Microfinanzas.BusinessEntities;

namespace MyCommerce.Windows.Stores.Views
{
    public partial class ProductStockMassiveView : BaseForm, IProductStockMassive
    {
        private ProductStockMassivePresenter vPresenter;
        private bool isUpdate = false;

        public ProductStockMassiveView()
        {
            InitializeComponent();
            vPresenter = new ProductStockMassivePresenter((IProductStockMassive)this);
        }

        #region IStore Members

        private ProductStock vobjProductStock;
        public ProductStock objProductStock
        {
            get
            {
                vobjProductStock = new ProductStock();
                if (isUpdate)
                {
                    vobjProductStock.IdProductStock = (long)gridStore.ActiveRow.Cells["IdProductStock"].Value;
                }
                vobjProductStock.IdProduct = (long)cmbProduct.SelectedRow.Cells["IdProduct"].Value;
                vobjProductStock.IdSupplier = (long)cmbSupplier.SelectedValue;
                vobjProductStock.IdStore = (long)cmbStore.SelectedValue;
                vobjProductStock.SerialNumber = txtSerialNumber.Text;
                vobjProductStock.IdProductPrice = (long)cmbProductPrice.SelectedRow.Cells["IdProductPrice"].Value;
                vobjProductStock.Quantity = Convert.ToInt32(txtCountProduct.Text);
                return vobjProductStock;
            }
            set
            {
                if (String.IsNullOrEmpty(cmbProduct.ValueMember) == false)
                    cmbProduct.Value = value.IdProduct;
                if (String.IsNullOrEmpty(cmbSupplier.ValueMember) == false)
                    cmbSupplier.SelectedValue = value.IdSupplier;
                if (String.IsNullOrEmpty(cmbStore.ValueMember) == false)
                    cmbStore.SelectedValue = value.IdStore;
                txtSerialNumber.Text = value.SerialNumber;
                if (String.IsNullOrEmpty(cmbProductPrice.ValueMember) == false)
                    cmbProductPrice.Value = value.IdProductPrice;
                txtCountProduct.Text = value.Quantity.ToString();
            }
        }


        public new event EventHandler OnLoad;

        public event OnSaveEventHandler OnSave;

        public event OnEntityEventHandler OnEdit;

        public event OnEntityEventHandler OnDelete;

        #endregion

        private void StoreView_Load(object sender, EventArgs e)
        {
            OnLoad(sender, e);
            Modify(false);
        }

        private void tsNewButtonToolStrip_Click(object sender, EventArgs e)
        {
            commonButtonsToolStrip1.ToggleButtons();
            Modify(true);
        }

        private void tsEditButtonToolStrip_Click(object sender, EventArgs e)
        {
            OnEdit(sender, (long)gridStore.ActiveRow.Cells["IdProductStock"].Value);
            isUpdate = true;

            commonButtonsToolStrip1.ToggleButtons();
            Modify(true);
        }

        private void tsDeleteButtonToolStrip_Click(object sender, EventArgs e)
        {
            if (Context.ShowQuestionMessageBox("¿Desea eliminar a este parametro?", "Eliminar parametro") == System.Windows.Forms.DialogResult.Yes)
            {
                OnDelete(sender, (long)gridStore.ActiveRow.Cells["IdProductStock"].Value);
                Context.ShowInformationMessageBox("El parametro se eliminó correctamente.", "Eliminar parametros");
            }   
        }

        private void tsSaveButtonToolStrip_Click(object sender, EventArgs e)
        {
            if (!DataAutoValidate())
                return;

            if (!isUpdate)
            {
                if (OnSave != null)
                {
                    OnSave(this, true);
                    Context.ShowInformationMessageBox("La información se guardó correctamente.", "Creación/Edición de usuarios.");
                }

            }
            else
            {
                if (OnSave != null)
                {
                    OnSave(this, false);
                    Context.ShowInformationMessageBox("La información se actualizó correctamente.", "Creación/Edición de usuarios.");
                }
            }
            isUpdate = false;
            commonButtonsToolStrip1.ToggleButtons();
            Modify(false);
            CleanScreen();
        }

        private void tsCancelButtonToolStrip_Click(object sender, EventArgs e)
        {
            isUpdate = false;
            commonButtonsToolStrip1.ToggleButtons();
            Modify(false);
            CleanScreen();
        }

        private void tsCloseButtonToolStrip_Click(object sender, EventArgs e)
        {
            this.Close();
        }


        private Boolean DataAutoValidate()
        {
            bool result = true;
            StringBuilder message = new StringBuilder();

            if (cmbProduct.SelectedRow == null)
            {
                result = false;
                message.AppendLine("Debe seleccionar un producto.");
            }

            if (String.IsNullOrEmpty(cmbSupplier.ValueMember))
            {
                result = false;
                message.AppendLine("Debe seleccionar un proveedor.");
            }

            if (cmbProductPrice.SelectedRow == null)
            {
                result = false;
                message.AppendLine("Debe seleccionar un precio del producto.");
            }

            if (String.IsNullOrEmpty(txtCountProduct.Text.Trim()))
            {
                result = false;
                message.AppendLine("Debe colocar la cantidad del producto a almacenar.");
            }

            if (!result)
            {
                Context.ShowErrorMessageBox(message.ToString(), "Validación de datos.");
            }

            return result;
        }

        private void Modify(bool edit)
        {
            gridStore.Enabled = !edit;
            cmbProduct.Enabled = edit;
            cmbProductPrice.Enabled = edit;
            cmbStore.Enabled = edit;
            cmbSupplier.Enabled = edit;
            txtSerialNumber.Enabled = edit;
            txtCountProduct.Enabled = edit;
        }

        private void CleanScreen()
        {
            txtCountProduct.Text = string.Empty;
            txtSerialNumber.Text = string.Empty;
        }
        
        #region IProductStock Members
        
        public List<Product> colProduct
        {
            set 
            {
                cmbProduct.ValueMember = "IdProduct";
                cmbProduct.DisplayMember = "Model";
                cmbProduct.DataSource = value;
            }
        }

        public List<ProductPrice> colProductPrice
        {
            set
            {
                cmbProductPrice.ValueMember = "IdProductPrice";
                cmbProductPrice.DisplayMember = "SalePrice";
                cmbProductPrice.DataSource = value;
            }
        }

        public List<Store> colStore
        {
            set
            {
                cmbStore.ValueMember = "IdStore";
                cmbStore.DisplayMember = "Name";
                cmbStore.DataSource = value;
            }
        }

        public List<Supplier> colSupplier
        {
            set
            {
                cmbSupplier.ValueMember = "IdSupplier";
                cmbSupplier.DisplayMember = "Name";
                cmbSupplier.DataSource = value;
            }
        }

        public long idProduct
        {
            get
            {
                if (cmbProduct.SelectedRow != null)
                {
                    return (long)cmbProduct.SelectedRow.Cells["IdProduct"].Value;
                }
                return -1;
            }
        }

        public long idSupplier
        {
            get
            {
                if (String.IsNullOrEmpty(cmbSupplier.ValueMember) == false)
                {
                    return (long)cmbSupplier.SelectedValue;
                }
                return -1;
            }
        }

        public event EventHandler OnChargeProductPrice;

        #endregion

        private void cmbProduct_RowSelected(object sender, Infragistics.Win.UltraWinGrid.RowSelectedEventArgs e)
        {
            OnChargeProductPrice(sender, e);
        }

        private void cmbSupplier_SelectedIndexChanged(object sender, EventArgs e)
        {
            OnChargeProductPrice(sender, e);
        }


        public List<ProductStock> colProductStockMassive
        {
            set
            {
                gridStore.DataSource = value;
            }
        }
    }
}
